package com.mukefile.service.impl;

import com.mukefile.dao.UserDao;
import com.mukefile.pojo.MukeFile;
import com.mukefile.pojo.User;
import com.mukefile.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author: kenewstar
 * @Description: 用户操作，业务逻辑层
 * 用户登录，用户修改密码，用户搜索文件功能
 * @Date:Created in 2020/5/4
 */
@Service("userService")
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    private Logger logger = Logger.getLogger(UserServiceImpl.class);

    @Autowired
    private UserDao userDao;

    @Override
    public User userLogin(User user) {
        logger.info("用户："+user.getUsername()+",发起了登录操作！");
        User user0 = this.userDao.selectUser(user);
        if (user0==null){
            //登录失败
            logger.info("用户："+user.getUsername()+",登录失败！");
        }else {
            logger.info("用户："+user.getUsername()+",登录成功！");
        }
        return user0;
    }

    @Override
    public int updatePwd(String pwd, User user) {
        logger.info("用户："+user.getUsername()+",发起了修改密码操作！");
        int i = this.userDao.updateUserPassword(pwd, user);
        if (i==0){
            //修改密码失败
            logger.info("用户："+user.getUsername()+",修改密码失败！");
        }else {
            logger.info("用户："+user.getUsername()+",修改密码成功！");
        }
        return i;
    }

    @Override
    public List<MukeFile> searchFile(String fileName, User user) {
        logger.info("用户："+user.getUsername()+",搜索文件，搜索关键字为："+fileName);

        List<MukeFile> list = this.userDao.selectFileByLike(fileName, user);
        if (list==null){
            logger.info("用户："+user.getUsername()+",未搜索到文件!");
        }else {
            logger.info("用户："+user.getUsername()+",文件搜索成功!");
        }
        return list;
    }

    @Override
    public void userExit(Map session) {
        logger.info("用户："+((User)session.get("user")).getUsername()+",退出了！");
        session.remove("user");
    }
}
